<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Advertisement;
use yii;


class AdvLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getAdv($query = [], $page = 1, $limit = 10)
    {
        $adv = Advertisement::find()->filterWhere($query)->orderBy('ad_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $adv->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $adv->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'adv/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function editAdvById($id, $params, $uploadresult)
    {
        if(!$adv = Advertisement::findOne($id)) return false;
        $adv->title = $params['title'];
        if($uploadresult)
            $adv->pic = $uploadresult;
        $adv->content = $params['content'];
        return $adv->save();
    }

    public function getById($id)
    {
        return Advertisement::find()->where(['ad_id' => $id])->asArray()->one();
    }


    public function saveStatus($id)
    {
        $adv = Advertisement::findOne($id);
        if($adv->status)
            $adv->status = 0;
        else
            $adv->status = 1;
        return $adv->save();
    }

    public function addAdv($params,$uploadresult){
        $adv = new Advertisement();
        $adv->title = $params['title'];
        $adv->pic = $uploadresult;
        $adv->position = $params['position'];
        $adv->content = $params['content'];
        return $adv->save();
    }


}